package com.example.developerlab;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;

public class CustomDialog {
	private AlertDialog mDialog;

	protected String picUrl = "";

	public void showDialogWithConfig(Activity activity) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity, R.style.Theme_AppCompat_Dialog);
		View view = View.inflate(activity, R.layout.layout_dialog, null);

		mDialog = alertDialog.setView(view).create();

		// 设置弹窗不可以由back键控制
		mDialog.setCancelable(false);

		// 设置弹窗周围有灰色遮罩层，且点击遮罩层后能够触发onCancelListener监听
		mDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
		mDialog.setCanceledOnTouchOutside(true);

		// 设置弹窗位置
		WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
		if (params != null) {
			params.gravity = Gravity.RIGHT | Gravity.TOP;
			mDialog.getWindow().setAttributes(params);
		}

		//各种事件监听
		ImageView ivClose = view.findViewById(R.id.iv_middle_close);
		ImageView backgroundPic = view.findViewById(R.id.iv_background_pic);

		PicUtils.downloadPic(activity, picUrl, backgroundPic, () -> mDialog.show());

		FrameLayout mainBody = view.findViewById(R.id.fl_main_body);
		mainBody.setOnClickListener(v -> {
			// Do someting you want
		});
		ivClose.setOnClickListener(v -> dismissDialog());
		mDialog.setOnCancelListener(v -> dismissDialog());
		mDialog.show();
	}

	protected void dismissDialog() {
		if (mDialog != null) {
			mDialog.dismiss();
		}
	}
}
